/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza.command;

import java.util.Map;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.globant.google.mendoza.MendozaServer;
import com.globant.google.mendoza.MendozaRequest;
import com.globant.google.mendoza.MendozaServerState;
import com.globant.google.mendoza.MendozaStatus;

/** Represents the status mendoza server command.
 */
public final class MendozaStatusCommand extends MendozaBaseCommand {

  /** The class logger.
   */
  private static Log log = LogFactory.getLog(MendozaStatusCommand.class);

  /** The mendoza server status.
   */
  private MendozaStatus status;

  /** The mendoza server status components.
   */
  private Map<String, String> statusComponents;

  /** Creates an instance of MendozaStatusCommand.
   *
   * @param mendozaRequest The mendoza server request.
   *
   * @param mendoza The mendoza server.
   *
   * @param mendozaState The mendoza server state.
   */
  public MendozaStatusCommand(
      final MendozaRequest mendozaRequest, final MendozaServer mendoza,
      final MendozaServerState mendozaState) {
    super(mendozaRequest, mendoza, mendozaState);
    setName("Status");
  }

  /** Executes the mendoza server login command.
   */
  public void execute() {
    log.trace("Entering execute");
    MendozaCommandResult result = new MendozaCommandResult();
    status = getMendoza().getMendozaStatus();
    boolean commandsIssued = status.getCommands().size() > 0;
    String resultString = "";
    if (commandsIssued) {
      createCommandsMap();
      String component =
        getMendozaRequest().getRequestParameters().getProperty("get");
      if (component == null
          || "".equals(component)
          || !statusComponents.containsKey(component)) {
        /* All the status information */
        resultString = status.toString();
      } else {
        resultString = statusComponents.get(component);
      }
    } else {
      resultString = "The server is waiting for commands";
    }
    result.setSuccess(resultString);
    setResult(result);
    log.trace("Leaving execute");
  }

  /** Creates the mendoza server commands map.
   */
  private void createCommandsMap() {
    log.trace("Entering createCommandsMap");
    statusComponents = new HashMap<String, String>();
    statusComponents.put("gon", status.getGoogleOrderNumber());
    String risk = status.getRiskInformationNotificationMsg();
    statusComponents.put("risk", risk.replace("><", ">\n<"));
    String non = status.getNewOrderNotificationMsg();
    statusComponents.put("non", non.replace("><", ">\n<"));
    statusComponents.put("fin", status.getFinancialStates().toString());
    statusComponents.put("ful", status.getFulfillmentStates().toString());
    statusComponents.put("cmd", status.getCommands().toString());
    statusComponents.put("mc", status.getMerchantCalculations().toString());
    log.trace("Leaving createCommandsMap");
  }
}

